class Youth < ActiveRecord::Base
	belongs_to :youth_status
	belongs_to :youth_group
	belongs_to :region
	belongs_to :institution
	belongs_to :data_collector
	has_many :data_collections, :dependent => :destroy
	has_many :youth_notes, :dependent => :destroy

	after_create :after_create_proc
	after_update :after_update_proc

  attr_writer :current_user



  def update_data_collections
    DataCollectionType.all_types.values.each do |data_collection_type|
      if (!has_data_collection(data_collection_type) and
            is_valid_data_collection(data_collection_type))
        DataCollectionManager.create_data_collection(self, data_collection_type)
      end
    end
    
    self.data_collections.each do |existing_data_collection|
      if (!is_valid_data_collection(existing_data_collection.data_collection_type) and
            is_uninitiated_data_collection(existing_data_collection))
        existing_data_collection.destroy
      end
    end
  end


  
  private

  def after_create_proc
    YouthNote.create(:youth_id => self.id, :signature => @current_user.login, :note => "SYSTEM MESSAGE: Youth created")
  end


  def after_update_proc
    track_changes # Logs important changes in Youth's notes.
    update_data_collections
  end


  def is_valid_data_collection(type)
    return false unless (self.youth_status.name == YouthStatus.all_statuses[:POSITIVE_CONSENT])
        
    if (type.to_s == DataCollectionType.all_types[:PRE_TREATMENT])      
      return true
    else
      return !institution_placement_date.nil?
    end
  end


  #  def get_t1_filled_out_date
  #    # TODO
  #    self.data_collections.each do |dc|
  #      if (dc.data_collection_wave.name == DataCollectionWave::T1 &&
  #            dc.data_collection_type.name == DataCollectionType::PRE_TREATMENT)
  #        dc.questionnaires.each do |q|
  #          if (q.questionnaire_name.name == QuestionnaireName::PARENT ||
  #                q.questionnaire_name.name == QuestionnaireName::YOUTH)
  #            q.questionnaire_events.each do |e|
  #              return e.event_date if (e.questionnaire_event_type.name == QuestionnaireEventType::FILLED_OUT_BY_INTERVIEWER ||
  #                  e.questionnaire_event_type.name == QuestionnaireEventType::FILLED_OUT_BY_RESPONDENT)
  #            end
  #          end
  #        end
  #      end
  #    end
  #
  #    return nil
  #  end

  
  def has_data_collection(type)
    self.data_collections.each do |dc|
      return true if (dc.data_collection_type.name == type)
    end

    return false
  end




  # Will not remove data collections in progress or completed
  def is_uninitiated_data_collection(data_collection)
    return data_collection.calculate_status == DataCollection::STATUS_NOT_STARTED
  end
	

  # To keep a history of important changes, they are identified here
  # and logged in the youth notes.
  def track_changes
    YouthNote.create(:youth_id => self.id, :signature => @current_user.login,
      :note => format("SYSTEM MESSAGE: Status changed from '%s' to '%s'",
        YouthStatus.find(self.youth_status_id_change[0]).name,
        YouthStatus.find(self.youth_status_id_change[1]).name)) if (self.youth_status_id_changed?)

    YouthNote.create(:youth_id => self.id, :signature => @current_user.login,
      :note => format("SYSTEM MESSAGE: Group changed from '%s' to '%s'",
        YouthGroup.find(self.youth_group_id_change[0]).name,
        YouthGroup.find(self.youth_group_id_change[1]).name)) if (self.youth_group_id_changed?)

    if (self.institution_id_changed? && self.institution_id_change[0].nil?)
      YouthNote.create(:youth_id => self.id, :signature => @current_user.login,
        :note => format("SYSTEM MESSAGE: Institution assigned: '%s'",
          Institution.find(self.institution_id_change[1]).name))
    elsif (self.institution_id_changed? && self.institution_id_change[1].nil?)
      YouthNote.create(:youth_id => self.id, :signature => @current_user.login,
        :note => format("SYSTEM MESSAGE: Institution unassigned: '%s'",
          Institution.find(self.institution_id_change[0]).name))
    elsif (self.institution_id_changed?)
      YouthNote.create(:youth_id => self.id, :signature => @current_user.login,
        :note => format("SYSTEM MESSAGE: Institution changed from '%s' to '%s'",
          Institution.find(self.institution_id_change[0]).name,
          Institution.find(self.institution_id_change[1]).name))
    end
  end

end

